package cn.z2huo.knowledge.concurrency.virtualthread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * @author z2huo
 */
@Slf4j
class VirtualThreadCreateTest {

    public void threadOfVirtual() {
        Thread.ofVirtual()
                .name("my-virtual-thread")
                .start(() -> log.info("this is a virtual thread, Thread.ofVirtual()"));
    }

    public void executorsNewVirtualThreadExecutor() {
        try (ExecutorService virtualExecutor = Executors.newVirtualThreadPerTaskExecutor()) {
            virtualExecutor.submit(() -> log.info("this is a virtual thread, newVirtualThreadPerTaskExecutor()"));
        }
    }

    public void threadStartVirtualThread() {
        Thread.startVirtualThread(() -> {
            log.info("this is a virtual thread, startVirtualThread()");
        });
    }

    public static void main(String[] args) throws InterruptedException {
        VirtualThreadCreateTest test = new VirtualThreadCreateTest();
        test.threadOfVirtual();

        test.executorsNewVirtualThreadExecutor();

        test.threadStartVirtualThread();

        TimeUnit.SECONDS.sleep(1);
        //Thread.currentThread().join();
    }

}
